package xyz.naokeziteng.data_structure.graph_theory;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author hawk
 * @date 2022/10/8
 * @desc 邻接矩阵
 **/
public class AdjacencyMatrix {

    //顶点数量
    private int V;
    //边数量
    private int E;

    //矩阵
    private int[][] adj;

    //建图 O(E)
    public AdjacencyMatrix(String filename) {
        File file = new File(filename);
        try (Scanner scanner = new Scanner(file)) {
            V = scanner.nextInt();
            if (V < 0) {
                throw new IllegalArgumentException("V must be non-negative");
            }
            adj = new int[V][V];
            E = scanner.nextInt();
            if (E < 0) {
                throw new IllegalArgumentException("E must be non-negative");
            }
            for (int i = 0; i < E; i++) {
                int a = scanner.nextInt();
                validateVertex(a);
                int b = scanner.nextInt();
                validateVertex(b);
                //自环边
                if (a == b) {
                    throw new IllegalArgumentException("self loop is detected!");
                }
                //平行边
                if (adj[a][b] == 1) {
                    throw new IllegalArgumentException("parallel edges are detected!");
                }
                adj[a][b] = 1;
                adj[b][a] = 1;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public int V() {
        return V;
    }

    public int E() {
        return V;
    }

    //是否存在边 O(1)
    public boolean hasEdge(int v, int w) {
        validateVertex(v);
        validateVertex(w);
        return adj[v][w] == 1;
    }

    //获取相邻的边 O(V)
    public Iterable<Integer> adj(int v) {
        return adjList(v);
    }

    private List<Integer> adjList(int v) {
        validateVertex(v);
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < V; i++) {
            if (adj[v][i] == 1) {
                result.add(i);
            }
        }
        return result;
    }

    //求一个顶点的度(相邻边的个数) O(V)
    public int degree(int v) {
        return adjList(v).size();
    }

    private void validateVertex(int v) {
        if (v < 0 || v >= V) {
            throw new IllegalArgumentException("vertex " + v + "is invalid");
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("V = %d, E = %d\n", V, E));
        for (int i = 0; i < V; i++) {
            for (int j = 0; j < V; j++) {
                sb.append(String.format("%d ", adj[i][j]));
            }
            sb.append("\n");
        }
        return sb.toString();
    }


    public static void main(String[] args) {
        AdjacencyMatrix adjacencyMatrix = new AdjacencyMatrix("src/main/resources/g.txt");
        System.out.print(adjacencyMatrix.toString());
    }
}
